package com.example.administrator.nurse.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

import com.example.administrator.nurse.common;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.ArrayList;


/**
 *  冠宏很累-2016.4.12
 *
 * */

public class InternetHelper {

    private static int timeOutCount = 0;
    //private static final int IO_BUFFER_SIZE = 10 * 1024;
	// 检测网络连接
	public static boolean checkConnection(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo != null) {
			return networkInfo.isAvailable();
		}
		return false;
	}

    public static boolean checkConnection() {
        ConnectivityManager connectivityManager = (ConnectivityManager) common.context.getSystemService(common.context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo == null) {
            Log.d("zzzzz","无法连接网络");
            Toast.makeText(common.context,"无法连接网络!",Toast.LENGTH_LONG).show();
            return false;
        }else{
            return true;
        }
    }
	/*public static boolean isWifi(Context mContext) {
		ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		if (activeNetInfo != null && activeNetInfo.getTypeName().equals("WIFI")) {
			return true;
		}
		return false;
	}*/

    //第一个是 httpURLconnection
    /** 同步机制错误，synchronized 导致 下面的方法有时候还没处理完，就被请求，导致无法请求 */
    public static String getStringFromUrlOnHttpRULconnection(String url) throws IOException{
        String json = null;
        URL myUrl = null;
        try {
            myUrl = new URL(url);
            HttpURLConnection myUrlConnect = (HttpURLConnection) myUrl.openConnection();
            myUrlConnect.setConnectTimeout(10 * 1000);//7秒超时
            myUrlConnect.setRequestMethod("GET");

            BufferedReader reader = new BufferedReader(new InputStreamReader(myUrlConnect.getInputStream(), "UTF-8"), 8);
            StringBuilder info = new StringBuilder();//定义字符容器
            String line = null;//用来保存提取出的每行数据
            while ((line = reader.readLine()) != null) {//保证读到的每行数据不为null
                info.append(line + "\n");//每行相加
            }
            json = info.toString();
            reader.close();
        }catch(SocketTimeoutException e){
            timeOutCount++;
            if(timeOutCount==6){
                return "timeout";
            }
            return getStringFromUrlOnHttpRULconnection(url);
        } catch (Exception e) {
            Log.d("zzzzz",e.toString());
        }
        return json;
    }

    public static String exchangeUserAccount(String account){
        if(account!=null) {
            if (account.contains("com")) {
                account = account.replace("com", "");
                if (account.contains(".")) {
                    account = account.replace(".", "");
                }
                if (account.contains("@")) {
                    account = account.replace("@", "");
                }
            }
        }
        return account;
    }

    /** httpUrlConnection 的 post */
    public static String postData
    (
            String url,
            String[] keys,
            String[] values
    ){
        int length = keys.length;
        StringBuffer data = new StringBuffer();
        for(int i=0;i<length;i++){
            data.append(keys[i]+"="+values[i]+"&"); /** 注意，httpUrlC 是以 = 链接键值的 */
        }
        try {
            URL myUrl = new URL(url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) myUrl.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setDoOutput(true);
            //httpURLConnection.setRequestProperty("Connection","Keep-Alive");
            httpURLConnection.setRequestProperty("Charset", "UTF-8"); /** 解决乱码 */
            httpURLConnection.connect(); /** 连接后，往流里写 */
            OutputStream outputStream = httpURLConnection.getOutputStream();
            Log.d("zzzzz",data.toString().getBytes().toString());
            outputStream.write(data.toString().getBytes());
            outputStream.flush();
            outputStream.close();
            /** 获取输出的信息 */
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"UTF-8"),8);
            StringBuilder stringBuffer = new StringBuilder();
            String temp;
            while ((temp = bufferedReader.readLine())!=null){
                stringBuffer.append(temp);
            }
            bufferedReader.close();
            httpURLConnection.disconnect();
            return stringBuffer.toString();
        }catch (Exception e){
            Log.d("zzzzz","postData exception "+e.toString());
        }finally {
            System.gc();
        }
        return null;
    }

    public static String uploadPic(String uploadUrl,String filename,Bitmap bit){ //同一时间只能上传一张，必须加 synchronized
        String end = "\r\n";
        String twoHyphens = "--";
        String boundary = "******";
        int compress=100;
        Log.d("zzzzz","pic url "+uploadUrl);
        try{
            URL url = new URL(uploadUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            // 设置每次传输的流大小，可以有效防止手机因为内存不足崩溃
            // 指定流的大小，当内容达到这个值的时候就把流输出，即是块大小设置
            httpURLConnection.setChunkedStreamingMode(256 * 1024);// 256K
            // 允许输入输出流
            httpURLConnection.setConnectTimeout(10*1000);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setUseCaches(false);
            // 使用POST方法
            httpURLConnection.setRequestMethod("POST");
            // 默认是短链接
            httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            httpURLConnection.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);

            // 发送头
            DataOutputStream dos = new DataOutputStream(httpURLConnection.getOutputStream());
            dos.writeBytes(twoHyphens + boundary + end);
            dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
                    + filename.substring(filename.lastIndexOf("/") + 1)
                    + "\""
                    + end);
            dos.writeBytes(end);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bit.compress(Bitmap.CompressFormat.JPEG, compress, baos);
            while (baos.toByteArray().length / 1024 > 700) {
                Log.d("zzzzz","compress time ");
                baos.reset();
                compress -= 10;
                if(compress==0){
                    bit.compress(Bitmap.CompressFormat.JPEG, compress, baos);
                    break;
                }
                bit.compress(Bitmap.CompressFormat.JPEG, compress, baos);
            }
            InputStream fis = new ByteArrayInputStream(baos.toByteArray());
           // FileInputStream fis = new FileInputStream(srcPath);
            byte[] buffer = new byte[10*1024]; // 8k+2k
            int count = 0;
            // 读取文件
            while ((count = fis.read(buffer)) != -1) {
                // 传图片比特
                dos.write(buffer, 0, count);
            }
            fis.close();
            dos.writeBytes(end);
            dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
            dos.flush();

            // 获取返回值
            InputStream is = httpURLConnection.getInputStream();
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            StringBuilder info = new StringBuilder();//定义字符容器
            String line = null;//用来保存提取出的每行数据
            while ((line = br.readLine()) != null) {//保证读到的每行数据不为null
                info.append(line + "\n");//每行相加
            }
            String json = info.toString();
            Log.d("zzzzz", "send pic result "+json);
            dos.close();
            is.close();
            return json.trim();
        } catch (Exception e){
            e.printStackTrace();
            Log.d("zzzzz", e.toString());
            return null;
        }
    }

    public static  int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
        }
        return inSampleSize;
    }

    //这个函数用来初始化数组列表ArrayList
    public static ArrayList<NameValuePair> init(
            String content, String account,String postid,String username,String location,int ul ,int isHasPic,int picNum,int model,int music,int video) {
        ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();//定义一个键值对来行的数组容器
        if(content!=null){
            data.add(new BasicNameValuePair("content", content));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(account!=null){
            data.add(new BasicNameValuePair("account", account));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(postid!=null){
            data.add(new BasicNameValuePair("postid", postid));//上面我定义了一个mysql命令，这里是数据库名字，两个由参数传入，增加了灵活性
        }
        if(username!=null){
            data.add(new BasicNameValuePair("username", username));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(location!=null){
            data.add(new BasicNameValuePair("userlocation", location));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        data.add(new BasicNameValuePair("ul", "" + ul));
        data.add(new BasicNameValuePair("haspic",""+isHasPic));
        data.add(new BasicNameValuePair("picnum",""+picNum));
        data.add(new BasicNameValuePair("model",""+model));
        data.add(new BasicNameValuePair("havemusic",""+music));
        data.add(new BasicNameValuePair("havevideo",""+video));
        return data;//返回设置好了容器
    }



    public static ArrayList<NameValuePair> initTalk(String va, String a,String p,String c,
                                                    String name,String owercontent,int type,int kind,String postAccount) {
        ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();//定义一个键值对来行的数组容器
        if(postAccount!=null){
            data.add(new BasicNameValuePair("postAccount", postAccount));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(va!=null){
            data.add(new BasicNameValuePair("viewaccount", va));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(a!=null){
            data.add(new BasicNameValuePair("account", a));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(p!=null){
            data.add(new BasicNameValuePair("postid", p));//上面我定义了一个mysql命令，这里是数据库名字，两个由参数传入，增加了灵活性
        }
        if(c!=null){
            data.add(new BasicNameValuePair("content", c));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(name!=null){
            data.add(new BasicNameValuePair("username", name));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(owercontent!=null){
            data.add(new BasicNameValuePair("owercontent", owercontent));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        data.add(new BasicNameValuePair("type", ""+type));
        data.add(new BasicNameValuePair("kind", ""+kind));
        return data;//返回设置好了容器
    }


    public static ArrayList<NameValuePair> initSecondTalk(String va, String a,String p,String c,String name,String position,String owercontent) {
        ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();//定义一个键值对来行的数组容器
        if(va!=null){
            data.add(new BasicNameValuePair("viewaccount", va));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(a!=null){
            data.add(new BasicNameValuePair("account", a));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(p!=null){
            data.add(new BasicNameValuePair("postid", p));//上面我定义了一个mysql命令，这里是数据库名字，两个由参数传入，增加了灵活性
        }
        if(c!=null){
            data.add(new BasicNameValuePair("content", c));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(name!=null){
            data.add(new BasicNameValuePair("username", name));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(position!=null){
            data.add(new BasicNameValuePair("position", position));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(owercontent!=null){
            data.add(new BasicNameValuePair("owercontent", owercontent));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }


        return data;//返回设置好了容器
    }

    public static ArrayList<NameValuePair> initGood(
            String goodtype, String content,String position,String account,String va,String postid,String aod,String username) {
        ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();//定义一个键值对来行的数组容器
        if(goodtype!=null){
            data.add(new BasicNameValuePair("goodtype", goodtype));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(content!=null){
            data.add(new BasicNameValuePair("content", content));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(position!=null){
            data.add(new BasicNameValuePair("position", position));//上面我定义了一个mysql命令，这里是数据库名字，两个由参数传入，增加了灵活性
        }
        if(account!=null){
            data.add(new BasicNameValuePair("account", account));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(va!=null){
            data.add(new BasicNameValuePair("viewaccount", va));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(postid!=null){
            data.add(new BasicNameValuePair("postid", postid));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(aod!=null){
            data.add(new BasicNameValuePair("aod", aod));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(username!=null){
            data.add(new BasicNameValuePair("username", username));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        return data;//返回设置好了容器
    }

    public static ArrayList<NameValuePair> initGoodNew(
            String content,String username,int id) {
        ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();//定义一个键值对来行的数组容器
        if(content!=null){
            data.add(new BasicNameValuePair("content", content));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(username!=null){
            data.add(new BasicNameValuePair("username", username));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }

        if(id!=-1){
            data.add(new BasicNameValuePair("id",""+id));
        }
        return data;//返回设置好了容器
    }

    public static ArrayList<NameValuePair> initCy(
            String project_name, String project_type,String project_word,
            String project_need,String project_connect,String project_content,
            String account,int project_postid,int project_hpic,int project_picn) {
        ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();//定义一个键值对来行的数组容器
        if(project_name!=null){
            data.add(new BasicNameValuePair("project_name", project_name));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(project_type!=null){
            data.add(new BasicNameValuePair("project_type", project_type));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(project_word!=null){
            data.add(new BasicNameValuePair("project_word", project_word));//上面我定义了一个mysql命令，这里是数据库名字，两个由参数传入，增加了灵活性
        }
        if(project_need!=null){
            data.add(new BasicNameValuePair("project_need", project_need));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(project_connect!=null){
            data.add(new BasicNameValuePair("project_connect", project_connect));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(project_content!=null){
            data.add(new BasicNameValuePair("project_content", project_content));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(account!=null){
            data.add(new BasicNameValuePair("account", account));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
            data.add(new BasicNameValuePair("project_postid", ""+project_postid));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写

            data.add(new BasicNameValuePair("project_hpic", ""+project_hpic));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写

            data.add(new BasicNameValuePair("project_picn", ""+project_picn));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写


        return data;//返回设置好了容器
    }

    public static ArrayList<NameValuePair> initMine(
            String title, String sign,String content,
            String account,int postid,int video) {
        ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();//定义一个键值对来行的数组容器
        if(title!=null){
            data.add(new BasicNameValuePair("title", title));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(sign!=null){
            data.add(new BasicNameValuePair("sign", sign));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(content!=null){
            data.add(new BasicNameValuePair("content", content));//上面我定义了一个mysql命令，这里是数据库名字，两个由参数传入，增加了灵活性
        }
        if(account!=null){
            data.add(new BasicNameValuePair("account", account));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        data.add(new BasicNameValuePair("postid", ""+postid));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写

        //data.add(new BasicNameValuePair("hpic", ""+hpic));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写

        //data.add(new BasicNameValuePair("picn", ""+picn));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写

        data.add(new BasicNameValuePair("hvideo", ""+video));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写


        return data;//返回设置好了容器
    }

    public static ArrayList<NameValuePair> initSet(String content,String oldpw,String newpw,String account,int kind) {
        ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();//定义一个键值对来行的数组容器

        if(account!=null){
            data.add(new BasicNameValuePair("account", account));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(content!=null) {
            data.add(new BasicNameValuePair("content", content));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(kind!=0) {
            data.add(new BasicNameValuePair("kind", ""+kind));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(newpw!=null) {
            data.add(new BasicNameValuePair("newpw", ""+newpw));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(oldpw!=null) {
            data.add(new BasicNameValuePair("oldpw", ""+oldpw));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }

        return data;//返回设置好了容器
    }


    //------------------------------------------------------------------------------------------------------

    public static ArrayList<NameValuePair> initPostDataSets(String[] keys,String[] values) {
        ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();//定义一个键值对来行的数组容器
        for(int i=0;i<keys.length;i++){
            data.add(new BasicNameValuePair(keys[i],values[i]));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        return data;//返回设置好了容器
    }

    public static String postDataToServer(String url,String[] keys,String[] values) {
        try {
            UrlEncodedFormEntity body = new UrlEncodedFormEntity(initPostDataSets(keys,values),"UTF-8");
            return sendCommonFunction(url,body);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    //---------------------------------------------------------------------------------------------------------------------

    public static ArrayList<NameValuePair> initHotLiveSet(String cid, String a,String content,String name) {
        ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();//定义一个键值对来行的数组容器

        if(cid!=null){
            data.add(new BasicNameValuePair("content_id",cid));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(a!=null) {
            data.add(new BasicNameValuePair("account", a));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(content!=null) {
            data.add(new BasicNameValuePair("content", content));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(name!=null) {
            data.add(new BasicNameValuePair("name", name));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        return data;//返回设置好了容器
    }

    public static ArrayList<NameValuePair> initCollect(String content, String account,String myaccount,String postid,String kind,String type,String picnum) {
        ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();//定义一个键值对来行的数组容器

        if(account!=null){
            data.add(new BasicNameValuePair("account",account));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(myaccount!=null) {
            data.add(new BasicNameValuePair("myaccount", myaccount));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(content!=null) {
            data.add(new BasicNameValuePair("content", content));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(postid!=null) {
            data.add(new BasicNameValuePair("postid", postid));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(kind!=null) {
            data.add(new BasicNameValuePair("kind", kind));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(type!=null) {
            data.add(new BasicNameValuePair("type", type));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(picnum!=null) {
            data.add(new BasicNameValuePair("picnum", picnum));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        return data;//返回设置好了容器
    }

    public static ArrayList<NameValuePair> initJuBao(String content, String account,String postid,String picnum) {
        ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();//定义一个键值对来行的数组容器

        if(account!=null){
            data.add(new BasicNameValuePair("account",account));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(content!=null) {
            data.add(new BasicNameValuePair("content", content));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(postid!=null) {
            data.add(new BasicNameValuePair("postid", postid));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        if(picnum!=null) {
            data.add(new BasicNameValuePair("picnum", picnum));//这里设置php文件接收的Order，例如 $_REQUEST["Order"]，根据你的php自己定义的来写
        }
        return data;//返回设置好了容器
    }

    public static String sendUserSetInfoToServer(String url,String content,String oldpw,String newpw,String account,int kind){
        try {
            UrlEncodedFormEntity body = new UrlEncodedFormEntity(initSet(content, oldpw, newpw, account, kind),"UTF-8");
            return sendCommonFunction(url,body);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String sendJuBaoInfoToServer(String url,String content,String account,String postid,int picnum){
        try {
            UrlEncodedFormEntity body = new UrlEncodedFormEntity(initJuBao(content, account, postid, "" + picnum),"UTF-8");
            return sendCommonFunction(url,body);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String sendCollectInfoToServer(String url,String content,String account,String myaccount,String postid,int kind,int type,int picnum){
        try {
            UrlEncodedFormEntity body = new UrlEncodedFormEntity(initCollect(content, account, myaccount, postid, "" + kind, "" + type, "" + picnum),"UTF-8");
            return sendCommonFunction(url,body);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String sendTalkContentToServer(String postAccount,String url,String va, String a,String postid,
                                                 String content,String name,String owercontent,int type,int kind) {
        try {
            UrlEncodedFormEntity body = new UrlEncodedFormEntity(initTalk(va, a, postid, content, name, owercontent, type, kind, postAccount),"UTF-8");
            return sendCommonFunction(url,body);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String sendSecondHotLiveTalkContentToServer(String url,String cid, String a,String content,String name) {
        try {
            UrlEncodedFormEntity body = new UrlEncodedFormEntity(initHotLiveSet(cid, a, content, name),"UTF-8");
            return sendCommonFunction(url,body);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String sendSecondTalkContentToServer(
            String url,String va, String a,String postid,String content,String name,String position,String owercontent) {
        try {
            UrlEncodedFormEntity body = new UrlEncodedFormEntity(initSecondTalk(va, a, postid, content, name, position, owercontent),"UTF-8");
            return sendCommonFunction(url,body);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String sendPostContentToServer(
            String url,String content, String account,String postid,String username,
            String location,int ul,int isHasPic,int picNum,int model,int havemusic,int havevideo) {
        try {
            UrlEncodedFormEntity body = new UrlEncodedFormEntity(
                    init(content, account, postid, username, location, ul, isHasPic, picNum, model, havemusic, havevideo),"UTF-8");
            return sendCommonFunction(url,body);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String sendCyPostContentToServer(
            String url,String project_name, String project_type,String project_word,
            String project_need,
            String project_connect,String project_content,String account,
            int project_postid,int project_hpic,int project_picn) {
        try {
            UrlEncodedFormEntity body = new UrlEncodedFormEntity(
                    initCy(project_name, project_type, project_word, project_need,
                            project_connect, project_content, account,
                            project_postid, project_hpic, project_picn),"UTF-8");
            return sendCommonFunction(url,body);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String sendMinePostContentToServer(
            String url,String title,
            String sign,
            String content,
            String account,
            int video,
            int postid) {
        try {
            UrlEncodedFormEntity body = new UrlEncodedFormEntity(
                    initMine(title, sign, content, account, postid, video), "UTF-8");
            return sendCommonFunction(url,body);
        } catch (Exception e) {
            Log.d("zzzzz", "mine send post exceptionis " + e.toString());
        }
        return null;
    }

    public static String sendGoodContentToServer(
            String url,String goodtype, String content,String position,String account,String va,String postid,String aod,String username) {
        try {
            UrlEncodedFormEntity body = new UrlEncodedFormEntity(initGood(goodtype,content,position,account,va,postid,aod,username),"UTF-8");
            return sendCommonFunction(url,body);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String sendGoodContentToServerNew(
            String url,String content,String username,int id) {
        try {
            UrlEncodedFormEntity body = new UrlEncodedFormEntity(initGoodNew(content, username, id),"UTF-8");
            return sendCommonFunction(url,body);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }


    private static String sendCommonFunction(String url,UrlEncodedFormEntity body){
        String json="-1";
        InputStream GetContentFromDb = null;//定义一个保存输入流的变量
        try {
            HttpClient http = new DefaultHttpClient();//开启http服务
            HttpPost post = new HttpPost(url);//传入url，初始化要post数据的url
            post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");//也是为了解决 post中文的乱码问题
            post.setEntity(body);
            HttpResponse response = http.execute(post);//这里才正真地进行访问，带着上面设置的数据
            HttpEntity responseFromDb = response.getEntity();//接受返回的实体
            GetContentFromDb = responseFromDb.getContent();//接受实体内容，并保存到输入流对象中
        } catch (Exception e) {
            Log.d("zzzzz", e.toString());
        }
        try {
            assert GetContentFromDb != null;
            BufferedReader reader = new BufferedReader(new InputStreamReader(GetContentFromDb, "UTF-8"), 8);//取回的内容，按照 utf8
            //上面的这句作用是把输入流里面的内容进行编码，第二个最好设置UTF-8，要和你的mysql表的一样，如果用iso-8859-1可能会抛出乱码错误
            StringBuilder info = new StringBuilder();//定义字符容器
            String line = null;//用来保存提取出的每行数据
            while ((line = reader.readLine()) != null) {//保证读到的每行数据不为null
                info.append(line + "\n");//每行相加
            }
            GetContentFromDb.close();//关闭
            json = info.toString();//数据转化
        } catch (Exception e) {
            Log.d("*****",e.toString());
        }
        Log.d("*****", "3");
        return json;
    }

    static class  MyInputStream extends FilterInputStream {

        public MyInputStream(InputStream inputStream) {
            super(inputStream);
        }

        @Override
        public long skip(long n) throws IOException {
            long totalBytesSkipped = 0L;
            while (totalBytesSkipped < n) {
                long bytesSkipped = in.skip(n - totalBytesSkipped);
                if (bytesSkipped == 0L) {
                    int by_te = read();
                    if (by_te < 0) {
                        break; // we reached EOF
                    } else {
                        bytesSkipped = 1; // we read one byte
                    }
                }
                totalBytesSkipped += bytesSkipped;
            }
            return totalBytesSkipped;
        }
    }
}
